﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Web7.Tests.Web7
{
    /// <summary>
    /// SequentialGuidFixture 的摘要说明
    /// </summary>
    [TestClass]
    public class SequentialGuidFixture
    {
        /// <summary>
        /// 顺序生成GUID
        /// </summary>
        [TestMethod]
        public void GUIDOrder()
        {
            List<Guid> sorted = new List<Guid>();
            List<Guid> unSorted = new List<Guid>();
            for (int i = 1; i <= 10000; i++)
            {
                var guid = SequentialGuid.NewGuid();
                sorted.Add(guid);
                unSorted.Add(guid);
            }
            sorted.Sort();  //顺序排序
            for (int i = 0; i < 10000; i++)
            {
                Assert.AreEqual(sorted[i], unSorted[i]);
            }
        }

        /*
        /// <summary>
        /// 压缩26位guid
        /// </summary>
        [TestMethod]
        public void CompressionGuid()
        {
            Dictionary<string, string> a = new Dictionary<string, string>();

            for (int i = 0; i < 10000; i++)
            {
                var guid = SequentialGuid.NewGuid();
                var guid2 = Convert36.GetGuidNo(guid);

                a.Add(guid2, null);
                Assert.AreEqual(Convert36.GetGuid(guid2), guid);
            }
        }*/
    }

    /*
    /// <summary>
    /// Convert36 的摘要说明
    /// </summary>
    public class Convert36
    {
        public Convert36()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

        private const string BASE_CHAR = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        //转换为段字符
        private static string GetLongNo(UInt64 num, int length)
        {
            string str = "";
            while (num > 0)
            {
                int cur = (int)(num % 36);
                str = BASE_CHAR[cur] + str;
                num = num / 36;
            }
            if (str.Length > length)
            {
                str = str.Substring(str.Length - length);
            }
            else
            {
                str = str.PadLeft(length, '0');
            }

            return str;
        }

        //解析段字符
        private static UInt64 GetLongNum(string strNo)
        {
            UInt64 num = 0;
            for (int i = 0; i < strNo.Length; i++)
            {
                num += (UInt64)BASE_CHAR.IndexOf(strNo[i]) * (UInt64)Math.Pow(BASE_CHAR.Length, strNo.Length - i - 1);
            }

            return num;
        }

        /// <summary>
        /// 压缩GUID
        /// </summary>
        /// <param name="g"></param>
        /// <returns></returns>
        public static string GetGuidNo(Guid g)
        {
            string s = g.ToString().Replace("-", "").ToUpper();
            string s1 = s.Substring(0, 16);
            string s2 = s.Substring(16);
            UInt64 l1 = UInt64.Parse(s1, System.Globalization.NumberStyles.HexNumber);
            UInt64 l2 = UInt64.Parse(s2, System.Globalization.NumberStyles.HexNumber);
            string str1 = GetLongNo(l1, 13);
            string str2 = GetLongNo(l2, 13);

            return str1 + str2;
        }

        /// <summary>
        /// 获取GUID
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static Guid GetGuid(string str)
        {
            if (str.Length != 26)
            {
                throw new ArgumentException("字符串错误！长度必须是26位！");
            }
            string s1 = str.Substring(0, 13);
            string s2 = str.Substring(13);
            UInt64 l1 = GetLongNum(s1);
            UInt64 l2 = GetLongNum(s2);
            string str1 = l1.ToString("X");
            string str2 = l2.ToString("X");
            string strGuid = str1.PadLeft(16, '0');
            strGuid += str2.PadLeft(16, '0');
            Guid g = new Guid(strGuid);
            return g;
        }
    }
     */
}
